﻿using AutoMapper;
using MediatR;
using T_Rbac_APi.Application.Command.DepartmentCommand;
using T_Rbac_Demain;
using T_Rbac_Rep.Interface;

namespace T_Rbac_APi.Application.CommandHandler.DepartmentHandler
{
    public class DepartmentAddHandler : IRequestHandler<DepartmentAddCommand, int>
    {
        private readonly IMapper mapper;
        private readonly IDapartment dapartment;

        public DepartmentAddHandler(IMapper mapper, IDapartment dapartment)
        {
            this.mapper = mapper;
            this.dapartment = dapartment;
        }

        public async Task<int> Handle(DepartmentAddCommand request, CancellationToken cancellationToken)
        {
            if (request.Dpid>0)
            {
                var lists = await dapartment.GetAloneByid(request.Dpid);
                request.Dpname = lists.Dname;
            }
            request.CreateDate = DateTime.Now;
            request.CreateBy = "小明";
            request.IsDel = false;
            var list = mapper.Map<Dapartment>(request);
            return await dapartment.Add(list);
        }
    }
}
